<?php

index();
function index() {
	DbInstance::get_update_db_sql();
}

class DbInstance {

	public static function get_replace_db_sql() {

		$tbl_name = "user";
		$info = array(id => 1, username => 'admin');
		$result = "";
		if (is_array($info) && !empty($info)) {
			$i = 0;
			foreach ($info as $key => $val) {
				$fields[$i] = $key;
				$values[$i] = $val;
				$i++;
			}
			$s_fields = "(" . implode(",", $fields) . ")";
			$s_values = "('" . implode("','", $values) . "')";
			$sql = "insert into  
                       $tbl_name  
                       $s_fields  
                   values  
                       $s_values";
			$result = $sql;
		} else {
			$result = false;
		}
		print_r($result);
	}

	/**
	 * 获取更新SQL语句
	 *
	 * @param    string     $tbl_name   表名
	 * @param    array      $info       数据
	 * @param    array      $condition  条件
	 */
	public static function get_update_db_sql() {
		$tbl_name = "user";
		$info = array(id => 1, username => 'admin', truename => '', age => 0, des => null,sex=>true);
		$condition = "";
		$result = "";
		$i = 0;
		$data = '';
		if (is_array($info) && !empty($info)) {
			foreach ($info as $key => $val) {
				//val不为null
				if (isset($val)) {
					$val = $val;
					if ($i == 0 && $val !== null) {
						$data = $key . "='" . $val . "'";
					} else {
						$data .= "," . $key . " = '" . $val . "'";
					}
					$i++;
				}
			}
			$sql = "update " . $tbl_name . " set " . $data . " where " . $condition;
			$result = $sql;
		} else {
			$result = false;
		}
		print_r($result);
	}

}
?>